﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TripleGameServices
{
    public class TripleGameDAL
    {
        /*ADD*/
        public static int AddPlayer(string name)
        {
            PlayerDTO newPlayer = new PlayerDTO();
            newPlayer.Name = name;
            TripleGameDBEntities entity = new TripleGameDBEntities();
            entity.AddToPlayers(newPlayer);
            entity.SaveChanges();
            return newPlayer.PlayerId;
        }

        public static int AddPendingGame(int playerID)
        {
            PendingGame game = new PendingGame();
            game.PlayerId = playerID;
            TripleGameDBEntities entity = new TripleGameDBEntities();
            entity.PendingGames.AddObject(game);
            entity.SaveChanges();

            return game.PendingGameId;
        }

        public static int AddActiveGame(int playerAId, int playerBId)
        {
            ActiveGame activeGame = new ActiveGame();
            activeGame.PlayerAId = playerAId;
            activeGame.PlayerBId = playerBId;
            TripleGameDBEntities entity = new TripleGameDBEntities();
            entity.ActiveGames.AddObject(activeGame);
            entity.SaveChanges();

            return activeGame.ActiveGameId; 
        }

        public static void AddGameMove(string startCell, string endCell, bool isPlayerA, int activeGameId)
        {
            GameMove gameMove = new GameMove();
            gameMove.ActiveGameId = activeGameId;
            gameMove.StartCell = startCell;
            gameMove.EndCell = endCell;
            gameMove.IsPlayerA = isPlayerA;
            TripleGameDBEntities entity = new TripleGameDBEntities();
            entity.AddToGameMoves(gameMove);
            entity.SaveChanges();
        }

        /*GET*/
        public static PlayerDTO GetPlayerById(int playerID, TripleGameDBEntities entity)
        {
            PlayerDTO player = entity.Players.Where(p => p.PlayerId == playerID).FirstOrDefault();
            return player;
        }

        public static ActiveGame GetActiveGameByID(int activeGameId, TripleGameDBEntities entity)
        { 
            ActiveGame activeGame = entity.ActiveGames.Where(ag => ag.ActiveGameId == activeGameId).FirstOrDefault();
            return activeGame;
        }

        public static int GetActiveGameIDByPlayerID(int playerID, TripleGameDBEntities entity)
        {
            int gameID = entity.ActiveGames.Where(ag => ag.PlayerAId == playerID || ag.PlayerBId == playerID).
                Select(ag => ag.ActiveGameId).FirstOrDefault();
            return gameID;
        }

        public static PendingGame GetPendingGameByID(int pendingGameID, TripleGameDBEntities entity)
        {
            PendingGame game = entity.PendingGames.Where(pg => pg.PendingGameId == pendingGameID).FirstOrDefault();
            return game;
        }
        
        /*DELETE*/
        public static void DeletePlayer(int playerID, TripleGameDBEntities entity)
        {
            TripleGameDAL.DeletePendingGameByPlayerID(playerID, entity);
            int activeGameID = TripleGameDAL.GetActiveGameIDByPlayerID(playerID, entity);
            TripleGameDAL.DeleteMovesByGameID(activeGameID, entity);
            TripleGameDAL.DeleteActiveGameByID(activeGameID, entity);
            PlayerDTO player = TripleGameDAL.GetPlayerById(playerID, entity);
            if (player == null)
            {
                return;
            }          
            entity.Players.DeleteObject(player);
            entity.SaveChanges();
        }

        public static void DeleteActiveGameByID(int gameID, TripleGameDBEntities entity)
        {
            ActiveGame game = TripleGameDAL.GetActiveGameByID(gameID, entity);
            if (game == null)
            {
                return;
            }
            entity.ActiveGames.DeleteObject(game);
            entity.SaveChanges();
        }

        public static void DeleteMovesByGameID(int gameID, TripleGameDBEntities entity)
        {
            var moves = entity.GameMoves.Where(m => m.ActiveGameId == gameID).ToList();
            if (moves.Count == 0)
            {
                return;
            }
            foreach (var move in moves)
            {
                entity.GameMoves.DeleteObject(move);
            }
            entity.SaveChanges();
        }

        public static void DeletePendingGameByPlayerID(int playerID, TripleGameDBEntities entity)
        {
            PendingGame game = entity.PendingGames.Where(pg => pg.PlayerId == playerID).FirstOrDefault();
            if (game == null)
            {
                return;
            }
            entity.PendingGames.DeleteObject(game);
            entity.SaveChanges();
        }

        public static void DeletePendingGameByID(int pendingGameID, TripleGameDBEntities entity)
        {
            PendingGame game = TripleGameDAL.GetPendingGameByID(pendingGameID, entity);
            if (game == null)
            {
                return;
            }
            entity.PendingGames.DeleteObject(game);
            entity.SaveChanges();
        }
    }
}